﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO;
using System.ComponentModel;
using Rio.Framework.Localisation;

namespace Rio.Framework.Pages.ChildWindows
{
    public partial class DownloadFolderChildWindow : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        private byte[] _zip;
        
        public object Sender { get; set; }

        #region Labels

        private int _totalFoldersProcessed;
        private int _totalFolders;
        private string _foldersProcessedLabel;
        private string _downloadArchiveButtonLabel;
        private string _windowTitle;
        
        public int TotalFoldersProcessed
        {
            get { return _totalFoldersProcessed; }
            set
            {
                _totalFoldersProcessed = value;
                OnPropertyChanged("TotalFoldersProcessed");
            }
        }
        public int TotalFolders
        {
            get { return _totalFolders; }
            set
            {
                _totalFolders = value;
                OnPropertyChanged("TotalFolders");
            }
        }
        public string FoldersProcessedLabel
        {
            get { return _foldersProcessedLabel; }
            set
            {
                _foldersProcessedLabel = value;
                OnPropertyChanged("FoldersProcessedLabel");
            }
        }
        public string WindowTitle
        {
            get { return _windowTitle; }
            set
            {
                _windowTitle = value;
                OnPropertyChanged("WindowTitle");
            }
        }
        public string DownloadArchiveButtonLabel
        {
            get { return _downloadArchiveButtonLabel; }
            set
            {
                _downloadArchiveButtonLabel = value;
                OnPropertyChanged("DownloadArchiveButtonLabel");
            }
        }

        #endregion

        /// <summary>
        /// Initializes a new instance of the <see cref="DownloadFolderChildWindow"/> class.
        /// </summary>
        public DownloadFolderChildWindow()
        {
            InitializeComponent();

            InitLabels();
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="DownloadFolderChildWindow"/> class.
        /// </summary>
        /// <param name="zip">The zip.</param>
        public DownloadFolderChildWindow(byte[] zip)
            : this()
        {
            _zip = zip;
        }

        /// <summary>
        /// Inits the labels.
        /// </summary>
        private void InitLabels()
        {
            FoldersProcessedLabel = Resx.GetLabel(ResxSearchPage.DOWNLOADFOLDER_FOLDERSPROCESSED);
            WindowTitle = Resx.GetLabel(ResxSearchPage.DOWNLOADFOLDER_TITLE);
            DownloadArchiveButtonLabel = Resx.GetLabel(ResxSearchPage.DOWNLOADFOLDER_SAVE);
        }

        /// <summary>
        /// Handles the Click event of the Button control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                SaveFileDialog zipFoldersSaveFileDialog = new SaveFileDialog()
                {
                    DefaultExt = ".zip",
                    Filter = "Zip|*.zip"
                };

                if (zipFoldersSaveFileDialog.ShowDialog() == true)
                {
                    using (Stream fs = zipFoldersSaveFileDialog.OpenFile())
                    {
                        fs.Write(_zip, 0, _zip.Length);
                        fs.Close();
                        fs.Dispose();

                        this.DialogResult = true;
                    }
                }
            }
            catch
            {
                this.DialogResult = false;
            }

            this.Close();
        }

        /// <summary>
        /// Handles the KeyUp event of the RadWindow control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Input.KeyEventArgs"/> instance containing the event data.</param>
        private void RadWindow_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            switch (e.Key)
            {
                case Key.Escape:
                    this.DialogResult = false;
                    this.Close();
                    break;
            }
        }

        /// <summary>
        /// Called when [property changed].
        /// </summary>
        /// <param name="propertyName">Name of the property.</param>
        protected void OnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}
